# -*- coding: utf-8 -*-
"""part2: 生产者-消费者
消费者
"""
import time
import pika


connection = pika.BlockingConnection(
    pika.ConnectionParameters(
        '172.22.23.252'
    )
)

channel = connection.channel()

channel.queue_declare(queue='task_queue', durable=True)  # durable 队列持久化
print('[*] Waiting for messages. To exit press CTRL+C')


def callback(ch, method, properties, body):
    print("[x] Received %r" % body)
    time.sleep(body.count(b'.'))
    print("[x] Done")
    channel.basic_ack(delivery_tag=method.delivery_tag)  # 确认消息被处理了


channel.basic_qos(prefetch_count=1)  # 在同一时刻，不要发送超过1条消息给一个工作者（worker）
channel.basic_consume(queue='task_queue', on_message_callback=callback)


channel.start_consuming()
